/**
 *  bookInfo.html 页面的javascript文件
 */

$().ready(function () {

    // ---------判断用户信息是否失效---------
    isUserInfoNull();

    // -----------------变量定义---------------------------
    var vehicleDeliveryId = getUrlParam().vehicleDeliveryId; // 车辆派送id
    var $userName = $("#user-name");
    var $userTelephone = $("#user-tel");
    var $save = $("#save");
    var $editUpdate = $("#edit-update");
    var $userRemarks = $("#user-remarks");
    var $update = $("#update");
    var $cancel = $("#cancel");

    var isBook; // 是否预约
    var isEvaluation; // 是否评价
    var isAdmin; // 是否是管理员
    var userName; // 用户名
    var userTelephone; // 用户电话
    var userRemarks; // 用户备注
    var bookId; // 用于预约id
    var agentFlag = 1; // 0 表示是被代理用户，1 表示没有被代理

    // --------------获取用户信息-------------------
    initPageInfo();

    // ------------点击按钮触发的事件----------------
    // ----保存-----
    $save.on("click", function () {
        // 显示提示框
        $("#loadingToast .weui-toast__content").text("努力提交中");
        $("#loadingToast").fadeIn(100);
        // --获取输入框信息
        userName = $userName.val();
        userTelephone = $userTelephone.val();
        userRemarks = $userRemarks.val();

        addInfo(userName, userTelephone, userRemarks, agentFlag, vehicleDeliveryId);

    });
    // ----switch按钮----
    $(".weui-switch-cp__input").on("click", function () {
        if ($(".weui-switch-cp__input").prop("checked")){
            console.log("被代理用户");
            // 给标记变量赋值
            agentFlag = 0;
            console.log(agentFlag);
            if (isBook === 0){
                // 代表已提交，获取数据
                getAgentInfo(bookId, agentFlag);
            } else {
                // 代表未提交，编辑数据， 将代理人数据清空
                $userName.val("");
                $userTelephone.val("");
                $userRemarks.val("");
            }

        } else {
            console.log("代理用户 ");
            agentFlag = 1;
            console.log(agentFlag);
            if (isBook === 0){
                getAgentInfo(bookId, agentFlag);
            } else {
                initPageInfo();
            }

        }
    });

    // ----更新----
    $update.on("click", function () {
        // 显示提示框
        $("#loadingToast .weui-toast__content").text("努力更新中");
        $("#loadingToast").fadeIn(100);

        // --获取输入框信息
        userName = $userName.val();
        userTelephone = $userTelephone.val();
        userRemarks = $userRemarks.val();
        updateInfo(userName, userTelephone, userRemarks, agentFlag, vehicleDeliveryId, bookId);
    });

    // ----取消----
    $cancel.on("click", function () {
        // 确认提示
        $("#cancelDialog").show();
    });

    // ----------提示框----------
    // --取消--
    $("#cancelNo").on("click", function () {
        $("#cancelDialog").hide();
    });
    // --确定--
    $("#cancelYes").on("click", function () {
        // 显示提示框
        $("#loadingToast .weui-toast__content").text("努力取消中");
        $("#loadingToast").fadeIn(100);
        $("#cancelDialog").hide();
        // --获取输入框信息
        userName = $userName.val();
        cancelInfo(bookId, vehicleDeliveryId, userName);
    });
    // --知道了--
    $("#iknow").on("click", function () {
        $("#dialog").hide();
    });

    // --------------获取用户信息-------------------
    function initPageInfo() {
        $.get("/bcs/user/getUserBaseInfo?vehicleDeliveryId=" + vehicleDeliveryId, function (result) {
            console.log(result);
            if (result.bookResult.errcode === 0){
                var targetData = result.bookResult.data;
                isBook = targetData.isBook;
                isEvaluation = targetData.isEvaluation;
                isAdmin = targetData.isAdmin;
                userName = targetData.userName;
                userTelephone = targetData.userTelephone;
                bookId = targetData.bookId;
                $userName.val(userName);
                $userTelephone.val(userTelephone);
                $userRemarks.val(targetData.userRemark);
                if (isBook === 0){
                    // 表示已预约
                    $save.hide();
                    $editUpdate.show();
                } else {
                    // 表示没有预约
                    $save.show();
                    $editUpdate.hide();
                }
            }
        });
    }

    // -----------用户提交预约车辆信息---------------
    function addInfo(name, telephone, remark, agentFlag, vDId){
        $.post("/bcs/user/add",{
            "userName" : name,
            "userTel" : telephone,
            "remark" : remark,
            "agentFlag" : agentFlag,
            "vehicleDeliveryId" : vDId
        },function (result) {
            console.log(result);
            if (result.bookResult.errcode === 0){
                $("#loadingToast").hide();
                $("#toast .weui-toast__content").text("提交成功");
                $("#toast").fadeIn(100);
                setTimeout(function(){
                    $("#toast").fadeOut(100);
                }, 2000);
                // 跳往首页
                window.location.href = '../index.html?vehicleDeliveryId=' + vehicleDeliveryId;
            }else if (result.bookResult.errcode === 2) {

                $("#loadingToast").hide();
                $("#toast .weui-toast__content").text("预约人数已达上限！");
                $("#toast").fadeIn(100);
                setTimeout(function(){
                    $("#toast").fadeOut(100);
                }, 2000);
            }else {
                $("#loadingToast").hide();
                $("#toast .weui-toast__content").text("提交失败");
                $("#toast").fadeIn(100);
                setTimeout(function(){
                    $("#toast").fadeOut(100);
                }, 2000);
            }
        });
    }

    // ----------用户更新预约车辆信息------------
    function updateInfo(name, telephone, remark, agentFlag, vDId, bookId) {
        $.post("/bcs/user/update",{
            "userName" : name,
            "userTel" : telephone,
            "remark" : remark,
            "agentFlag" : agentFlag,
            "vehicleDeliveryId" : vDId,
            "bookId" : bookId
        },function (result) {
            console.log(result);
            if (result.bookResult.errcode === 0){
                $("#loadingToast").hide();
                $("#toast .weui-toast__content").text("更新成功");
                $("#toast").fadeIn(100);
                setTimeout(function(){
                    $("#toast").fadeOut(100);
                }, 2000);
                // 跳往首页
                window.location.href = '../index.html?vehicleDeliveryId=' + vehicleDeliveryId;
            }else {
                $("#loadingToast").hide();
                $("#toast .weui-toast__content").text("更新失败");
                $("#toast").fadeIn(100);
                setTimeout(function(){
                    $("#toast").fadeOut(100);
                }, 2000);
            }
        });
    }

    // ----------用户取消预约车辆信息----------
    function cancelInfo(bookId, vDId, name) {
        $.post("/bcs/user/cancel", {
            "bookId" : bookId,
            "vehicleDeliveryId" : vDId,
            "userName" : name
        }, function (result) {
            console.log(result);
            if (result.bookResult.errcode === 0){
                $("#loadingToast").hide();
                $("#toast .weui-toast__content").text("取消成功");
                $("#toast").fadeIn(100);
                setTimeout(function(){
                    $("#toast").fadeOut(100);
                }, 2000);
                // 跳往首页
                window.location.href = '../index.html?vehicleDeliveryId=' + vehicleDeliveryId;
            }else {
                $("#loadingToast").hide();
                $("#toast .weui-toast__content").text("取消失败");
                $("#toast").fadeIn(100);
                setTimeout(function(){
                    $("#toast").fadeOut(100);
                }, 2000);
            }
        })
    }

    // ----------用户自己也预约时获取被代理用户信息----------
    function getAgentInfo(bookId, agent) {
        $.get("/bcs/user/getBaseInfo?bookId="+bookId+"&agentFlag="+agent, function (result) {
            // do something
            if (result.bookResult.errcode === 0){
                var targetData = result.bookResult.data;
                // 填充信息
                $userName.val(targetData.user_name);
                $userTelephone.val(targetData.user_telephone);
                $userRemarks.val(targetData.user_remarks);
            }else if (result.bookResult.errcode === 2){
                $("#dialog .weui-dialog__title").html("没有被代理的用户哦！");
                $("#dialog").show();
                // switch按钮 置为
                $(".weui-switch-cp__input").attr("checked", false);
            }
        })
    }

    // ----------判断用户是否失效---------
    function isUserInfoNull() {
        $.get("/bcs/user/isUserInfoNull", function (result) {

            if (result.bookResult.errcode === 1){
                // 用户信息已失效, 直接跳转
                $("#loadingToast .weui-toast__content").text("登录已过期,正在登录中");
                $("#loadingToast").fadeIn(100);
                // 使用微信oauth 的回调链接方式，进行再次登录
                window.location.href = result.bookResult.data;
            }
        });
    }

    // ----------用户没有预约时，获取被代理的用户----------

    // -------------获取url中的参数----------------
    function getUrlParam() {
        var url = window.location.href;
        console.log(url);
        if(url.indexOf("?") === -1) return;
        var urlSplit = url.split("?")[1];
        var res = [];
        if(urlSplit.indexOf("&") === -1) {
            var arr1 = urlSplit.split("=");
            res[arr1[0]] = arr1[1]
        } else {
            var arr2 = urlSplit.split("&");
            for(var i = 0; i < arr2.length; i++) {
                var key = arr2[i].split("=");
                res[key[0]] = key[1]
            }
        }
        return res
    }
});